package com.mall.xiaomi.controller;

import com.github.pagehelper.PageInfo;
import com.mall.xiaomi.pojo.Product;
import com.mall.xiaomi.service.ProductService;
import com.mall.xiaomi.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ProductController
 * @Description TODO
 * @action luoliang
 * @Data 2021/2/25 16:42
 * @Version 1.0
 */

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ResultMessage resultMessage;
    @Autowired
    private ProductService productService;

    /**
     * 首页展示
     * @param categoryId
     * @return
     */
    @GetMapping("/category/limit/{categoryId}")
    public ResultMessage getProductByCategoryId(@PathVariable Integer categoryId) {
        List<Product> list = productService.getProductByCategoryId(categoryId);
        if (list != null){
            resultMessage.success("200",list);
            return resultMessage;
        }else {
            resultMessage.success("200",null);
            return resultMessage;
        }
    }

    /**
     * 首页下面商品的展示
     * @return
     */
    @GetMapping("/category/hot")
    public ResultMessage getHotProduct(){
        List<Product> hotProduct = productService.getHotProduct();
        if (hotProduct != null){
            resultMessage.success("200",hotProduct);
            return resultMessage;
        }else {
            resultMessage.success("200",null);
            return resultMessage;
        }
    }

    /**
     * 产品分类展示
     * @param currentPage
     * @param pageSize
     * @param categoryId
     * @return
     */
    @GetMapping("/page/{currentPage}/{pageSize}/{categoryId}")
    public Map<String,Object> getProductByPage(@PathVariable String currentPage, @PathVariable String pageSize, @PathVariable String categoryId){
        PageInfo<Product> pageInfo = productService.getProductByPage(currentPage, pageSize, categoryId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","200");
        map.put("data",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    /**
     * 查询商品
     * @param
     * @return
     */
    @PostMapping("/getProductBySearch/{currentPage}/{pageSize}/{search}")
    public Map<String,Object> getProductByname(@PathVariable String currentPage,@PathVariable String pageSize,@PathVariable String search) {
        PageInfo<Product> pageInfo = productService.getProduct(currentPage,pageSize,search);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code","200");
        map.put("data",pageInfo.getList());
        map.put("total",pageInfo.getTotal());
        return map;
    }

     /** 通过商品id获取商品详细信息
     * @param productId 商品id
     * @return
     */

    @GetMapping("/{productId}")
    public ResultMessage getProductById(@PathVariable Integer productId){
        Product data =productService.getProductById(productId);
        resultMessage.success("001",data);
        return resultMessage;
    }
}
